package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Override
    public List<BaseAttribute> findAttributeByCategory1Id(Long category1Id) {
        List<BaseAttribute> baseAttributeList = baseAttributeMapper.findAttributeByCategory1Id(category1Id);

        return baseAttributeList;
    }

    @Override
    public List<JSONObject> getBaseCategoryList() {

        ArrayList<JSONObject> jsonObjects = new ArrayList<>();

        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

        Map<Long, List<BaseCategoryView>> collect = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : collect.entrySet()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("categoryId", longListEntry.getKey());
            jsonObject.put("categoryName", longListEntry.getValue().get(0).getCategory1Name());


            Map<Long, List<BaseCategoryView>> collect1 = longListEntry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            ArrayList<JSONObject> jsonObjects1 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> listEntry : collect1.entrySet()) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("categoryId", listEntry.getKey());
                jsonObject1.put("categoryName", listEntry.getValue().get(0).getCategory2Name());
                jsonObjects1.add(jsonObject1);


                Map<Long, List<BaseCategoryView>> collect2 = listEntry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
                ArrayList<JSONObject> jsonObjects2 = new ArrayList<>();
                for (Map.Entry<Long, List<BaseCategoryView>> entry : collect2.entrySet()) {
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("categoryId", entry.getKey());
                    jsonObject2.put("categoryName", entry.getValue().get(0).getCategory3Name());
                    jsonObjects2.add(jsonObject2);
                }

                jsonObject1.put("categoryChild", jsonObjects2);
            }

            jsonObject.put("categoryChild", jsonObjects1);

            jsonObjects.add(jsonObject);
        }


        return jsonObjects;


    }

    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    /**
     * 查询当前1级分类下包含子分类（二三级分类）
     *
     * @param category1Id
     * @return {"categoryId":1,"categoryName":"音乐",categoryChild:[{"categoryId":101,"categoryName":"音乐音效",categoryChild:[{..}]}]}
     */
    @Override
    public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
        //1.根据1级分类ID查询分类视图得到“1级”分类列表  封装1级分类对象
        LambdaQueryWrapper<BaseCategoryView> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryView::getCategory1Id, category1Id);
        List<BaseCategoryView> category1List = baseCategoryViewMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(category1List)) {
            String category1Name = category1List.get(0).getCategory1Name();
            //1.1 封装1级分类对象
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("categoryId", category1Id);
            jsonObject1.put("categoryName", category1Name);
            //2.处理二级分类
            List<JSONObject> jsonObject2List = new ArrayList<>();
            //2.1 对"1级"分类列表进行按2级分类ID分组
            Map<Long, List<BaseCategoryView>> map2 = category1List
                    .stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //2.2 遍历"2级"分类Map
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
                Long category2Id = entry2.getKey();
                String category2Name = entry2.getValue().get(0).getCategory2Name();
                //2.3 封装2级分类JSON对象
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId", category2Id);
                jsonObject2.put("categoryName", category2Name);
                //3.处理三级分类
                List<JSONObject> jsonObject3List = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : entry2.getValue()) {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
                    jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
                    jsonObject3List.add(jsonObject3);
                }
                jsonObject2.put("categoryChild", jsonObject3List);
                jsonObject2List.add(jsonObject2);
            }
            //2.4 将2级分类集合存入1级分类categoryChild中
            jsonObject1.put("categoryChild", jsonObject2List);
            return jsonObject1;
        }
        return null;
    }



    /**
     * 根据1级分类ID查询置顶3级分类列表
     *
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseCategory3> getTopBaseCategory3(Long category1Id) {
        //1.根据1级分类ID得到二级分类列表（获取二级分类ID列表）
        LambdaQueryWrapper<BaseCategory2> category2LambdaQueryWrapper = new LambdaQueryWrapper<>();
        category2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id, category1Id);
        category2LambdaQueryWrapper.select(BaseCategory2::getId);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(category2LambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(baseCategory2List)) {
            //2.根据二级分类ID列表得到置顶三级分类
            //2.1 获取所有2级分类ID
            List<Long> category2IdList = baseCategory2List
                    .stream()
                    .map(BaseCategory2::getId)
                    .collect(Collectors.toList());
            //2.2 构建查询三级分类条件
            LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
            baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop, 1);
            baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id, category2IdList);
            baseCategory3LambdaQueryWrapper.orderByAsc(BaseCategory3::getOrderNum);
            baseCategory3LambdaQueryWrapper.last("LIMIT 7");
            baseCategory3LambdaQueryWrapper.select(BaseCategory3::getId, BaseCategory3::getName, BaseCategory3::getCategory2Id);
            //2.3 查询得到7个置顶三级分类
            List<BaseCategory3> list = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
            return list;
        }
        return null;
    }



}
